/**
 * Created by XQ on 2015/8/11.
 */
(function QTip(window){

    var
        QTip = {};

    window.QTip = QTip;

    /**
     * 工具对象
     */
    QTip.Util = {};

    QTip.option = {
        "class" : "QTip",
        "wrapClass" : "QTip_Wrap",
        "noTitle" : false,
        "noFooter" : false,
        "width" : "",
        "height" : "",
        "contentType" : "txt"
    }

    function tip(title, content, _option){

        var option = $.extend(true, {}, QTip.option);	// 深层复制配置
        if(_option) {
            option = $.extend(true, option, _option);
        }

        var $titleContent = $("<span>").addClass("tipTitleContent"),
            $close = $("<span>").addClass("tipClose").html("关闭"),
            $title = $("<div>").addClass("tipTitle").append($titleContent).append($close),
            $content = $("<div>").addClass("tipContent"),
            $footer = $("<div>").addClass("tipFooter"),
            $DOM = $("<div>").addClass(option["class"]).append($title).append($content).append($footer),
            $wrap = $("<div>").addClass(option["wrapClass"]);

        $("body").append($wrap).append($DOM);

        this.getContent = function(){
            return $content;
        }

        if(option["noTitle"]){
            $title.remove();
        }

        $close.bind("click", function(){
            $wrap.hide();
            $DOM.hide();
        });

        if(option["noFooter"]){
            $footer.remove();
        } else {
            var $btn = $("<input type=\"button\">").val("确定");
            $footer.append($btn);
            $btn.bind("click", function(){
                $wrap.hide();
                $DOM.hide();
            });
        }

        if(option["width"]){
            $DOM.width(option["width"]);
        }

        if(option["height"]){
            $DOM.width(option["height"]);
        }

        this.setTitle = function(title){
            $titleContent.html(title);
        }

        this.refresh = function(title, content, _option){

            if(_option) {
                option = $.extend(true, option, _option);
            }

            if(!option["noTitle"]){
                this.setTitle(title);
            }

            if(option["contentType"] == "txt"){
                $content.html(content);
            } else if(option["contentType"] == "html"){
                $content.empty().append($(content));
            }
        }

        this.getDOM = function(){
            return $DOM;
        }

        this.show = function(){
            $wrap.show();
            $DOM.show();
        }

        this.hide = function(){
            $wrap.hide();
            $DOM.hide();
        }

        this.refresh(title, content, option);
        this.hide();
    }

    QTip.init = function(title, content, option){

        var tp = new tip(title, content, option);
        return tp;								//初始化完成，返回对象
    }
})(window);